/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webBeans;

import ejbean.UsuarioBeanLocal;
import entities.Usuario;
import java.util.Locale;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Alex
 */
@ManagedBean
@RequestScoped
public class LoginManagedBean {
    
    private FacesContext context;
    @EJB(mappedName = "UsuarioBean")
    private UsuarioBeanLocal usuarioBean;
    
    private Usuario usuario;
    private boolean logado;

    public boolean isLogado() {
        return logado;
    }

    public void setLogado(boolean logado) {
        this.logado = logado;
    }

    public LoginManagedBean() {
        context = FacesContext.getCurrentInstance();
        usuario = (Usuario) context.getExternalContext().getSessionMap().get("usuario");
        logado = true;
        if (usuario == null) {
            usuario = new Usuario();
            logado = false;
        }
        Locale idioma = (Locale)context.getExternalContext().getSessionMap().get("idioma");
        FacesContext.getCurrentInstance().getViewRoot().setLocale(idioma);
    }
    
    public Boolean logadoCliente(){
        boolean aux=usuario.getRolId().getRolId()==3;
        return logado && !aux;
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public String login() {
        String res = validate();

        if (!res.equals("ERROR")) {
            Usuario u= usuarioBean.findByEmailAndPassword(usuario.getEmail(), usuario.getPassword());
            
            if (u == null) {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "The user with the current password does not exist."));

                res = "ERROR";
            } else {
                logado=true;
                context.getExternalContext().getSessionMap().put("usuario", u);
            }
        }

        return res;
    }

    private String validate() {
        String res = "LOGIN";

        if (usuario.getEmail().equals("")) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "The Usuario field is required."));

            res = "ERROR";
        } else if (usuario.getPassword().equals("")) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "The Password field is required."));

            res = "ERROR";
        }

        return res;
    }
    
    
    public String logout(){
        context.getExternalContext().invalidateSession();
        logado=false;
        return "EXIT";
    }
    
    public void cambiarIdiomaEsp(){
        FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale("es"));
        System.out.println("Entra espanol");
        context.getExternalContext().getSessionMap().put("idioma", new Locale("es"));
    }
    public void cambiarIdiomaIng(){
        FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.ENGLISH);
        System.out.println("Entra ingles");
        context.getExternalContext().getSessionMap().put("idioma", Locale.ENGLISH);
    }
    
    public String enConstruccion(){
        return "CONSTRUCCION";
    }
}
